package com.bz.websocket;

import com.bz.websocket.utils.AssertUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 接入 WebSocket Service 的业务
 *
 * @author zhongyongbin
 * @since 2021/5/10
 */
public class WebSocketBiz {

    private static final Map<String, WebSocketBiz> INSTANCES = new HashMap<>();

    /**
     * 业务名
     */
    private String name;

    /**
     * 业务 URL
     */
    private String bizUrl;

    private WebSocketBiz() {
    }

    /**
     * 构建应用
     *
     * @param bizName 业务名称
     * @param bizUrl  业务地址
     * @return 应用
     */
    public static WebSocketBiz initializeBiz(String bizName, String bizUrl) {
        AssertUtil.requireNotBlank(bizName, () -> new IllegalArgumentException("App name can't be blank."));
        AssertUtil.requireNotBlank(bizUrl, () -> new IllegalArgumentException("Business url can't be empty."));

        WebSocketBiz websocketBiz = new WebSocketBiz();
        websocketBiz.name = bizName;
        websocketBiz.bizUrl = bizUrl;

        INSTANCES.put(bizName, websocketBiz);

        return websocketBiz;
    }

    /**
     * 获取应用
     *
     * @param bizName 应用名
     * @return 应用
     */
    public static WebSocketBiz getInstance(String bizName) {
        return INSTANCES.get(bizName);
    }

    /**
     * 获取业务 URL
     *
     * @return 业务 URL
     */
    public String getBizUrl() {
        return bizUrl;
    }

    public String getName() {
        return name;
    }
}
